package com.arui.yygh.hosp.controller;

import com.arui.common.utils.MD5;
import com.arui.yygh.common.exception.YyghException;
import com.arui.yygh.common.result.Result;
import com.arui.yygh.hosp.service.HospitalSetService;
import com.arui.yygh.model.hosp.Hospital;
import com.arui.yygh.model.hosp.HospitalSet;
import com.arui.yygh.vo.hosp.HospitalQueryVo;
import com.arui.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Random;

@Api(tags = "医院设置管理")
@RequestMapping("/admin/hosp/hospitalSet")
@RestController
//@CrossOrigin
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;
    //1.查询所有医院设置
    @ApiOperation(value = "获取所有医院设置信息")
    @GetMapping("/findAll")
    public Result findAll(){
        try {
            int i = 1/0;
        } catch (Exception e) {
            throw new YyghException("失败",200);
        }
        List<HospitalSet> list = hospitalSetService.list();
        Result<List<HospitalSet>> listResult = Result.ok(list);
        return listResult;
    }
    //2.逻辑删除医院设置
    @ApiOperation(value = "逻辑删除医院设置信息")
    @DeleteMapping("/{id}")
    public Result removeHospSet(@PathVariable Long id){
        boolean flag = hospitalSetService.removeById(id);
        if (flag){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    //3.条件查询带分页
    @PostMapping("/findPage/{current}/{limit}")
    public Result findPageHospSet(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        //创建Page对象，传递当前页，每页记录数
        Page<HospitalSet> page = new Page<>(current,limit);

        //调用方法实现分页查询，并且得有条件
        QueryWrapper queryWrapper = new QueryWrapper();
        String hoscode = hospitalSetQueryVo.getHoscode();
        String hosname = hospitalSetQueryVo.getHosname();
        if (!StringUtils.isEmpty(hoscode)){
            queryWrapper.eq("hoscode",hospitalSetQueryVo.getHoscode());
        }
        if (!StringUtils.isEmpty(hosname)){
            queryWrapper.like("hosname",hospitalSetQueryVo.getHosname());
        }



        Page<HospitalSet> pageHospitalSet = hospitalSetService.page(page,queryWrapper);

        //返回结果
        return Result.ok(pageHospitalSet);

    }
    //4.添加医院设置
    @PostMapping("/saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        //设置状态1使用，0不能使用
        hospitalSet.setStatus(1);
        //签名密钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        boolean save = hospitalSetService.save(hospitalSet);
        if (save){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    //5.根据id获取医院设置
    @GetMapping("/getHospSet/{id}")
    public Result getHospSet(@PathVariable("id") long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        if (hospitalSet!=null){
            return Result.ok(hospitalSet);
        }else {
            return Result.fail();
        }
    }

    //6。修改医院设置
    @PostMapping("/updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag){
            return Result.ok();
        }else{
            return Result.fail();
        }

    }

    //7.批量删除医院设置
    @DeleteMapping("/batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> list){
        boolean removeByIds = hospitalSetService.removeByIds(list);
        return Result.ok();

    }

    //8.医院设置的锁定与解锁
    @PutMapping("/lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable("id") long id,@PathVariable("status") Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        boolean b = hospitalSetService.updateById(hospitalSet);
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }

    }
    //9.发送签名密钥
    @PutMapping("/send/{id}")
    public Result send(@PathVariable long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO 发送短信
        return  Result.ok();
    }
}
